package com.aier.classes.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.classes.common.InviteRequestUtil;
import com.aier.classes.entity.RequestTeacher;
import com.aier.common.common.SystemDateFormatUtil;


public class RequestTeacherRowMapper implements RowMapper<RequestTeacher> {

    private int type;
    
    public static final int AND_REQUSETTEACHER = 1;
    
    public RequestTeacherRowMapper() {
    }

    public RequestTeacherRowMapper(int type) {
        this.type = type;
    }

    @Override
    public RequestTeacher mapRow(ResultSet rs, int rowNum) throws SQLException {
        RequestTeacher rt = parseRequestTeacher(new RequestTeacher(), rs);
        rt.setCtime_str(SystemDateFormatUtil.formatDate(rt.getCtime(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
        rt.setState_str(InviteRequestUtil.RequestStateMap.get(rt.getState()));
        
        switch (type) {
            case AND_REQUSETTEACHER:
                rt.setName_teacher(rs.getString("name_teacher"));
                break;
        }
        return rt;
    }

    public static RequestTeacher parseRequestTeacher(RequestTeacher s, ResultSet rs) throws SQLException {
        return parseRequestTeacher(s, rs, null);
    }

    public static RequestTeacher parseRequestTeacher(RequestTeacher s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setRtid(rs.getLong(su+"rtid"));
        } catch (SQLException e) {
            s.setRtid(null);
        }
        try {
            s.setUid(rs.getLong(su+"uid"));
        } catch (SQLException e) {
            s.setUid(null);
        }
        try {
            s.setTname(rs.getString(su+"tname"));
        } catch (SQLException e) {
            s.setTname(null);
        }
        try {
            s.setGid(rs.getLong(su+"gid"));
        } catch (SQLException e) {
            s.setGid(null);
        }
        try {
            s.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            s.setSid(null);
        }
        try {
            s.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            s.setCid(null);
        }
        try {
            s.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            s.setTid(null);
        }
        try {
            s.setContent(rs.getString(su+"content"));
        } catch (SQLException e) {
            s.setContent(null);
        }
        try {
            s.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            s.setState(null);
        }
        try {
            s.setCtime(rs.getTimestamp(su+"ctime"));
        } catch (SQLException e) {
            s.setCtime(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, RequestTeacher s) {
        par.put("rtid",s.getRtid());
        par.put("uid",s.getUid());
        par.put("tname",s.getTname());
        par.put("gid",s.getGid());
        par.put("sid",s.getSid());
        par.put("cid",s.getCid());
        par.put("tid",s.getTid());
        par.put("content",s.getContent());
        par.put("state",s.getState());
        par.put("ctime",s.getCtime());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, RequestTeacher s) {
        args.put("rtid",s.getRtid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final RequestTeacher s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "rtid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(RequestTeacher s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into request_teacher(");
        int count = 0;
        if (null != s.getUid()) {
            sql.append("uid,");
            count++;
        }
        if (null !=s.getTname()) {
            sql.append("tname,");
            count++;
        }
        if (null != s.getGid()) {
            sql.append("gid,");
            count++;
        }
        if (null != s.getSid()) {
            sql.append("sid,");
            count++;
        }
        if (null != s.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != s.getTid()) {
            sql.append("tid,");
            count++;
        }
        
        if (null != s.getContent()) {
            sql.append("content,");
            count++;
        }
        if (null != s.getState()) {
            sql.append("state,");
            count++;
        }
        if (null != s.getCtime()) {
            sql.append("ctime,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, RequestTeacher s)
        throws SQLException {
        int count = 1;
        if (null != s.getUid()) {
            ps.setLong(count++,s.getUid());
        }
        if (null != s.getTname()) {
            ps.setString(count++, s.getTname());
        }
        if (null != s.getGid()) {
            ps.setLong(count++,s.getGid());
        }
        if (null != s.getSid()) {
            ps.setInt(count++,s.getSid());
        }
        if (null != s.getCid()) {
            ps.setLong(count++,s.getCid());
        }
        if (null != s.getTid()) {
            ps.setLong(count++,s.getTid());
        }
        if (null != s.getContent()) {
            ps.setString(count++,s.getContent());
        }
        if (null != s.getState()) {
            ps.setShort(count++,s.getState());
        }
        if (null != s.getCtime()) {
            ps.setTimestamp(count++,s.getCtime());
        }


    }


}
